package com.sneakxy.mybatis.commons.repository.config;

import java.util.Collection;
import java.util.Collections;
import java.util.Optional;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
import org.springframework.data.repository.config.RepositoryConfigurationSource;
import org.springframework.data.repository.config.XmlRepositoryConfigurationSource;

/**
 * @author 潜行的青衣
 */
public class MybatisRepositoryConfigurationExtension extends RepositoryConfigurationExtensionSupport {
	
	public static final String DEFAULT_TRANSACTION_MANAGER_BEAN_NAME = "transactionManager";
	
	public static final String DEFAULT_SQL_SESSION_FACTORY_BEAN_NAME = "sqlSessionFactory";
	
	@Override
	public String getModuleName() {
		return "MYBATIS";
	}

	@Override
	public void postProcess(BeanDefinitionBuilder builder, RepositoryConfigurationSource source) {
		Optional<String> sqlSessionFactoryRef = source.getAttribute("sqlSessionFactoryRef");
		builder.addPropertyReference("sqlSessionFactory", 
				sqlSessionFactoryRef.isPresent() ? sqlSessionFactoryRef.get() : DEFAULT_SQL_SESSION_FACTORY_BEAN_NAME);
		Optional<String> transactionManagerRef = source.getAttribute("transactionManagerRef");
		builder.addPropertyValue("transactionManager",
				transactionManagerRef.isPresent() ? transactionManagerRef.get(): DEFAULT_TRANSACTION_MANAGER_BEAN_NAME);
		Optional<String> repositoryBaseClass = source.getRepositoryBaseClassName();
		if(repositoryBaseClass.isPresent()) {
			try {
				builder.addPropertyValue("repositoryBaseClass", Class.forName(repositoryBaseClass.get()));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfigurationSource config) {
		
	}

	@Override
	public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfigurationSource config) {
		
	}

	@Override
	public void registerBeansForRoot(BeanDefinitionRegistry registry,
			RepositoryConfigurationSource configurationSource) {
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport#getModulePrefix()
	 */
	@Override
	protected String getModulePrefix() {
		return this.getModuleName();
	}

	/* (non-Javadoc)
	 * @see org.springframework.data.repository.config.RepositoryConfigurationExtension#getRepositoryFactoryBeanClassName()
	 */
	@Override
	public String getRepositoryFactoryBeanClassName() {
		return MybatisTransactionalRepositoryFactoryBean.class.getName();
	}

	@Override
	protected Collection<Class<?>> getIdentifyingTypes() {
		return Collections.singletonList(Repository.class);
	}
	
}
